package org.sdau.rotten.library.controller;

import cn.hutool.core.convert.Convert;
import org.sdau.rotten.library.DTO.ChartDTO;
import org.sdau.rotten.library.entity.Book;
import org.sdau.rotten.library.entity.User;
import org.sdau.rotten.library.service.BookService;
import org.sdau.rotten.library.DTO.AppointExecution;
import org.sdau.rotten.library.DTO.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.*;

@Controller
@RequestMapping("/customer/book")
public class BookController {
    @Resource(name = "bookServiceImpl")
    private BookService bookService;

    @GetMapping("/chart")
    @ResponseBody
    public ChartDTO queryChart(@RequestParam("book_num") String bookNum, HttpServletRequest request) {
        Integer num = Convert.toInt(bookNum);
        List<Book> books = bookService.queryChart(num);
        int[][] ints = new int[num][2];

        ArrayList<String> strings = new ArrayList<>();
        for (Book book : books) {
            strings.add(book.getName());
        }

        for(int i = 0; i < num; i++) {
            for(int j = 0; j < 2; j++) {
                ints[i][j] = books.get(i).getNumber();
            }
            ints[i][0] = i + 1;


        }

        ChartDTO chartDTO = new ChartDTO();
        chartDTO.setBookName(strings);
        chartDTO.setValue(ints);
        request.getSession().setAttribute("chart",chartDTO);
        request.getSession().setAttribute("names",strings);

        System.out.println(strings);
        return chartDTO;
    }

    @ResponseBody
    @GetMapping("/appoint")
    private AppointExecution appoint(Model model, @RequestParam("book_id")long bookId,
                                     HttpServletRequest request){
        User User = (User) request.getSession().getAttribute("user");
        int studentId = User.getId();
        System.out.println(bookId+" "+studentId+"调用借阅成功了");
        AppointExecution appointExecution = bookService.appoint(bookId,studentId);
        model.addAttribute("appointExecution",appointExecution);

        return appointExecution;
    }

    @GetMapping(value = "/search")
    private ModelAndView search(ModelAndView model,@RequestParam("book_name")String bookName,
                                HttpServletRequest request) {
        List<Book> list = bookService.search(bookName);
        request.getSession().setAttribute("list",list);
        System.out.println(list.get(0).getName());
        model.addObject("list",list);

        model.setViewName("index");
        return model;
    }


    @ResponseBody
    @GetMapping( "/appointAgain")
    private JsonData appointAgain(
            Model model,
            @RequestParam("book_id")long bookId,
            HttpServletRequest request
    ) {
        User User = (User) request.getSession().getAttribute("user");
        int studentId = User.getId();
        Timestamp createTime = new Timestamp(new Date().getTime());
        System.out.println(bookId+" "+studentId+"调用续借成功了");
        int appointExecution = bookService.appointAgain(bookId,studentId,createTime);
        model.addAttribute("appointExecution",appointExecution);
        return JsonData.buildSuccess("续借成功");
    }
}
